﻿using AntMovement.Core;
using AntMovement.Core.Attributes;
using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Logic.Base;
using AntMovement.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Handle.BluingFilm
{
    [FlowAttributes(nameof(ConveyorBelt), 3)]
    public class ConveyorBelt : TaskBase
    {
        #region 轴变量
        private AxisMotion _axitZero;
        private AxisMotion _axitOne;
        #endregion
        #region 输入点
        private NormalOpen _中位物料检测光纤;
        private NormalOpen _物料到位光纤;
        #endregion
        #region 定时器

        #endregion
        #region 输出点
        private ICoil _废料回收电机;
        #endregion
        #region 上升沿
        private bool _fullChargeRisingEdge = false, _zeroRisingEdge = false, _oneRisingEdge = false;
        #endregion
        #region 私有变量
        private bool _homeAlarm = false;
        #endregion
        public ConveyorBelt(ITemporaryParameter temporary, IIOParameter iop, IAxisParameter axisp, IDialogHost dialogHost, ILog log) 
            : base(temporary, iop, axisp, dialogHost, log)
        {
            _axitZero = axisp.GetAxisMotion("输送带轴");
            _axitOne = axisp.GetAxisMotion("撕蓝膜轴");
            _中位物料检测光纤 = iop.GetNormalOpen("中位物料检测光纤");
            _废料回收电机 = iop.GetCoil("废料回收马达");
            _物料到位光纤 = iop.GetNormalOpen("物料到位光纤");

        }

        public override void Handle()
        {
            if (_temporary.SystemStatus.Auto)
            {
                if (!_homeAlarm)
                {
                    if (_step == 0)
                    {
                        if (_中位物料检测光纤.Execute())//中位检测有无物料
                        {
                            _step += 20;
                        }
                        else
                        {
                            //输送带转动
                            _axitZero.Continuous(true, false);
                            _axitOne.Continuous(true, false);
                            _废料回收电机.Execute(true);
                            NextStep();
                        }
                    }
                    if (_step == 10)
                    {
                        if (_中位物料检测光纤.Execute())//中位检测有无物料
                        {
                            _axitZero.Stop(1);
                            _axitOne.Stop(1);
                            _废料回收电机.Execute(false);
                            NextStep();
                        }
                    }
                    if (_step == 20)
                    {
                        //与视觉交互

                        if (!_中位物料检测光纤.Execute())
                        {
                            _step = 0;
                        }
                    }
                    if (_step == 30)
                    {
                        //if (!_axisStatusZ.SportStatus)
                        //{
                        //    _位移吸真空.Execute(true);
                        //    if (_位移负压信号.Execute() && _二次定位原点.Execute())
                        //    {
                        //        _axitZ.SetProfile();
                        //        _axitZ.Move(599, 1, 2);
                        //    }
                        //}
                        //if ((Math.Abs(_axisStatusZ.CurrentPostion - 599) <= 20) && !_axisStatusZ.SportStatus)
                        //{
                        //    _CachesCoordinate = _axisStatusX.CurrentPostion;
                        //    NextStep();
                        //}
                    }
                    if (_step == 40)
                    {
                        //if (_位移负压信号.Execute())
                        //{
                        //    _temporary.Status_模组运动.Permit_TakeMaterials = false;
                        //    if (!_axisStatusX.SportStatus && !_moveOKNGNegation)
                        //    {
                        //        bool bl = (new Random().Next() % 2) == 0;
                        //        if (bl)
                        //        {
                        //            //PointPositionMove(OK流拉);
                        //        }
                        //        else
                        //        {
                        //            //PointPositionMove(NG流拉);
                        //        }
                        //        _moveOKNGNegation = true;
                        //    }
                        //    else if (Math.Abs(_CachesCoordinate - _axisStatusX.CurrentPostion) >= 100)
                        //    {
                        //        _moveOKNGNegation = false;
                        //        NextStep();
                        //    }
                        //}
                        //else
                        //{
                        //    _物料检测失败 = true;
                        //    _dialogHost.DialogShow("位移负压信号检测异常!!!", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
                        //}
                    }
                }
                _temporary.OK满料 = _物料到位光纤.Execute();
                if (_temporary.OK满料 && !_fullChargeRisingEdge)
                {
                    _axitZero.Stop(1);
                    _axitOne.Stop(1);
                    _废料回收电机.Execute(false);
                    _fullChargeRisingEdge = true;
                }
                if (!_temporary.OK满料 && _fullChargeRisingEdge)
                {
                    _step = 0;
                    _fullChargeRisingEdge = false;
                }
            }
            if (_temporary.SystemStatus.ManualOperation)
            {

            }

        }

        public override void Resetting()
        {
            _step = 0;
        }

        public override void EmergencyStop()
        {
            _废料回收电机.Execute(false);
        }

        public override void SafeStop()
        {
            _废料回收电机.Execute(false);
        }
    }
}
